#include "CameraManager.hpp"
#include "camera_manage_type.h"
#include <chrono>
#include <iostream>
#include <thread>

// 定义一个结构体来包含摄像头的特定配置
struct CameraSpecificConfig {
    std::string deviceName;
    std::string filePath;
    std::string udpAddress;
    int udpPort;
    int h264Channel;
};

int main() {
    // 初始化一个多摄像头配置
    MultiCameraConfig config;
    config.cameras.reserve(2); // 预分配空间，现在是两个摄像头

    // 函数用于创建单个摄像头配置
    auto createCameraConfig = [](const CameraSpecificConfig &specificConfig) {
        CameraConfig camera;
        camera.baseConfig.enable = true;
        camera.baseConfig.deviceName = specificConfig.deviceName;
        camera.baseConfig.width = 1920;
        camera.baseConfig.height = 1080;
        camera.baseConfig.format = "NV12";
        camera.baseConfig.fps = 30;

        // 预分配编码器配置空间
        camera.encoderConfigs.reserve(2);

        // 初始化H264配置
        EncoderConfig h264Config;
        h264Config.channel = specificConfig.h264Channel;
        h264Config.width = 1920;
        h264Config.height = 1080;
        h264Config.in_format = "NV12";
        h264Config.out_format = "test_h264";
        h264Config.rotation = 0;
        camera.encoderConfigs.push_back(h264Config);

        // 初始化JPEG配置
        EncoderConfig jpegConfig;
        jpegConfig.channel = specificConfig.h264Channel + 1;
        jpegConfig.width = 1920;
        jpegConfig.height = 1080;
        jpegConfig.in_format = "NV12";
        jpegConfig.out_format = "test_jpeg";
        jpegConfig.rotation = 0;
        camera.encoderConfigs.push_back(jpegConfig);

        // 初始化存储配置
        camera.storeConfig.enable = 1;
        camera.storeConfig.pos = 0;
        camera.storeConfig.filePath = specificConfig.filePath;

        // 初始化传输配置
        camera.transportConfigs.reserve(2);

        TransportConfig udpConfig;
        udpConfig.enable = 1;
        udpConfig.protocol = Protocol::UDP;
        udpConfig.address = specificConfig.udpAddress;
        udpConfig.port = specificConfig.udpPort;
        udpConfig.direction = 0;
        camera.transportConfigs.push_back(udpConfig);

        TransportConfig socketConfig;
        socketConfig.enable = 1;
        socketConfig.protocol = Protocol::LOCAL_SOCKET;
        socketConfig.address = "/tmp/camera_socket";
        socketConfig.port = 0;
        socketConfig.direction = 1;
        camera.transportConfigs.push_back(socketConfig);

        return camera;
    };

    // 定义两个摄像头的特定配置
    CameraSpecificConfig camera1Config = {
        "/dev/video44",
        "/userdata/hx/nfs_code/build/jpeg",
        "172.16.31.46",
        4568,
        0};

    CameraSpecificConfig camera2Config = {
        "/dev/video22",
        "/userdata/hx/nfs_code/build/jpeg2",
        "172.16.31.46",
        4568,
        2};

    // 创建两个摄像头配置并添加到MultiCameraConfig中
    config.cameras.push_back(createCameraConfig(camera1Config));
    config.cameras.push_back(createCameraConfig(camera2Config));

    CameraManager cameraManager;
    std::cout << "Initializing camera manager...\n";

    // 初始化和启动摄像头管理器
    cameraManager.initialize(config);
    cameraManager.start();

    std::cout << "Camera manager initialized and started.\n";

    // 保持主线程活跃以继续处理帧
    while (true) {
        std::this_thread::sleep_for(std::chrono::seconds(2));
        std::cout << "Main thread still running...\n";
    }

    return 0;
}